package controllers;

import java.util.Date;
import java.util.List;

import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import models.sys.User;
import play.Logger;
import play.data.binding.As;
import ret.Ret;
import swagger.annotations.ApiInfo;
import swagger.annotations.Param;
import utils.SessionUtils;

@ApiInfo(desc = "用户模块接口")
public class Users extends SController {

    @ApiInfo(desc = "验证手机号码", consumes = {"application/x-www-form-urlencoded"})
    public static void validateMobile(@Param(name = "mobile") String mobile) {
        if (StringUtils.isEmpty(mobile) || mobile.length() != 11) {
            renderJSON(Ret.error("手机号码不正确"));
            return;
        }
        User user = null;
        if ((user = User.findByMobile(mobile)) == null) {
            user = new User();
            user.mobile = mobile;
        }
        user.validateCode = "1234";// RandomStringUtils.random(4,
        // "0123456789");
        user.validateExpr = DateUtils.addMinutes(new Date(), 15);
        Logger.info("mobile:%s,validate_code:%s", mobile, user.validateCode);
        user.save();
        renderJSON(Ret.ok("请查收验证码"));
    }

    @ApiInfo(desc = "用户注册", consumes = {"application/x-www-form-urlencoded"})
    public static void regiester(
            @Param(name = "mobile", in = "formData") String mobile,
            @Param(name = "passwd", in = "formData") String passwd,
            @Param(name = "nickname", in = "formData") String nickname,
            @Param(name = "valcode", in = "formData") String valcode) {

    }

    @ApiInfo(desc = "登陆验证")
    public static void auth(
            @Param(name = "mobile", desc = "手机号码", in = "formData") String mobile,
            @Param(name = "passwd", desc = "密码", in = "formData") String passwd) {
        User user = User.findByMobile(mobile);
        if (user != null
                && StringUtils.equals(user.passwd,
                        Md5Crypt.apr1Crypt(passwd, user.salt))) {
            SessionUtils.setCurrentUser(user);
//            if(request.isAjax())
            renderJSON(Ret.ok());
//            else
//                F.index();
        }
//        if(request.isAjax()) {
        renderJSON(Ret.error("登录失败"));
//        } else {
//            F.index();
//        }

    }

    @ApiInfo(desc = "修改密码", consumes = {"application/x-www-form-urlencoded"})
    public static void modifyPasswd(
            @Param(name = "mobile", in = "formData") String mobile,
            @Param(name = "passwd", in = "formData") String passwd,
            @Param(name = "valcode", desc = "验证码", in = "formData") String valcode) {

    }

    @ApiInfo(desc = "设置用户信息", consumes = {"application/x-www-form-urlencoded"})
    public static void modifyProfile(
            @Param(name = "nickname", in = "formData") String nickname,
            @Param(name = "sex", in = "formData") int sex,
            @Param(name = "birthday", in = "formData", desc = "2015-02-01") @As("yyyy-MM-dd") Date birthday,
            @Param(name = "career", in = "formData") String career,
            @Param(name = "workyears", in = "formData") int workyears
    ) {

    }

    public static void index() {
        List<User> users = User.all().fetch();
        render(users);
    }

}
